﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using carXapp2;
using carXapp2.classes;

namespace carXapp2.MiscPages
{
    public partial class AddEditFuel : PhoneApplicationPage
    {
        private string carID;
        private string fuelID;

        public AddEditFuel()
        {
            InitializeComponent();
            this.DataContext = App.ViewModel.SingleFuelRecord;
        }

        private void Menu_save(object sender, EventArgs e)
        {
            bool success = false;
            if (fuelID == null)
            {
                success = addFuelRecord();

            }
            else
            {
                success = UpdateFuelRecord();
 
            }
            if (NavigationService.CanGoBack && success)
            { NavigationService.GoBack(); }
        }

        private bool addFuelRecord()
        {
            float cost, fill;
            int miles;
            var rs1 = float.TryParse(txtCost.Text, out cost);
            var rs2 = float.TryParse(txtFill.Text, out fill);
            var rs3 = int.TryParse(txtMiles.Text, out miles);

            if (txtMiles.Text.Length > 0 && txtCost.Text.Length > 0 && txtFill.Text.Length > 0 && datePick.Value.ToString().Length > 0
                && rs1 && rs2 && rs3 )
            {

                App.ViewModel.SingleFuelRecord.DateStr = datePick.Value.Value.ToShortDateString();
                App.ViewModel.AddFuel(App.ViewModel.SingleFuelRecord);
                return true;
                    
            }
            else
            {
                MessageBox.Show("Incomplete or Incorrect data");
                return false; 
            }
        }

        private bool UpdateFuelRecord()
        {
             float cost, fill;
            int miles;
            var rs1 = float.TryParse(txtCost.Text, out cost);
            var rs2 = float.TryParse(txtFill.Text, out fill);
            var rs3 = int.TryParse(txtMiles.Text, out miles);

            if (txtMiles.Text.Length > 0 && txtCost.Text.Length > 0 && txtFill.Text.Length > 0 && datePick.Value.ToString().Length > 0
                && rs1 && rs2 && rs3)
            {
                App.ViewModel.SingleFuelRecord.DateStr = datePick.Value.Value.ToShortDateString();
                App.ViewModel.SaveChangesToDB();
                return true;
            }
            else
            {
                MessageBox.Show("Incorrect or Incomplete data");
                return false;

            }
        }

        private void PopulatePage()
        {
            
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            NavigationContext.QueryString.TryGetValue("carID", out carID);
            NavigationContext.QueryString.TryGetValue("FuelID", out fuelID);
            PopulatePage();
            base.OnNavigatedTo(e);
        }


    }
}